package com.daykw.ip.task;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.bson.Document;
import org.bson.types.ObjectId;
import org.nutz.dao.Chain;
import org.nutz.dao.Cnd;
import org.nutz.dao.entity.Record;
import org.nutz.dao.impl.NutDao;
import org.nutz.dao.pager.Pager;
import org.nutz.dao.sql.OrderBy;

import com.alibaba.fastjson.JSONObject;
import com.daykw.ip.model.TCommentUid;
import com.daykw.ip.util.Const;
import com.daykw.ip.util.MongoDBUtil;
import com.daykw.ip.util.SpringContextHolder;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;

public class StatUidsTask {
	private static Logger logger = Logger.getLogger(StatUidsTask.class);

	public void doStart() {

		try {

			// this.doStatUid();
			doFixNickName();
		} catch (Exception e) {
			logger.error("", e);
		}

	}//

	public void doFixNickName() {
		try {
			NutDao xhsDao = SpringContextHolder.getBean("xhsDao");

			Record re = xhsDao.fetch("t_fix_page", Cnd.where("type", "=", "user"));
			int pageSize = re.getInt("page_size");

			int page = re.getInt("page");

			for (int i = page; i <= 203; i++) {
				Pager pager = xhsDao.createPager(i, pageSize);
				Cnd c = Cnd.where("status", "=", 2).and("nick", "=", "");
				List<Record> res = xhsDao.query("t_author_task", c, pager);
				for (Record r : res) {
					String json = r.getString("json");
					if (StringUtils.isNotEmpty(json)) {
						JSONObject obj = JSONObject.parseObject(json);
						String nickname = obj.getString("nickname");

						xhsDao.update("t_author_task", Chain.make("nick", nickname),
								Cnd.where("id", "=", r.getLong("id")));

					}
				}
				xhsDao.update("t_fix_page", Chain.make("page", i), Cnd.where("type", "=", "user"));
				logger.info("修复完第" + i + "页");
			}

		} catch (Exception e) {

		}
	}

	public void doStatUid() {
		try {

			NutDao xhsDao = SpringContextHolder.getBean("xhsDao");
			MongoCollection<org.bson.Document> coll = MongoDBUtil.instance.getCollection(Const.MDB_DB, "comment");
			Record re = xhsDao.fetch("t_fix_page", Cnd.where("type", "=", "comment"));
			int pageSize = re.getInt("page_size");
			String lastid = re.getString("lastid");
			int page = re.getInt("page");
			int count = 0;
			while (lastid != null) {
				MongoCursor<Document> list = MongoDBUtil.instance.findByPage3(coll, lastid, pageSize);
				if (list == null || !list.hasNext()) {
					lastid = null;
					logger.info("全部修复完毕");
					break;
				}

				// String pdate = null;

				while (list.hasNext()) {
					Document _doc = list.next();
					ObjectId _id = _doc.getObjectId("_id");

					Document author = (Document) _doc.get("user");

					if (author != null) {
						String userid = author.getString("userid");

						if (StringUtils.isNotEmpty(userid)) {
							TCommentUid c = new TCommentUid();
							c.setCreate_time(new Date());
							c.setStatus(0);
							c.setUid(userid);

							try {
								xhsDao.fastInsert(c);
								if (xhsDao.fetch("t_author_task", Cnd.where("user_id", "=", userid)) != null) {
									xhsDao.update(TCommentUid.class, Chain.make("status", 1),
											Cnd.where("id", "=", c.getId()));
								}
							} catch (Exception e) {

							}

						}

					}

					if (count % 1000 == 0) {
						logger.info("count:" + count);
					}

					// 如果相等

					count++;

					lastid = _id.toString();
				}
				page++;
				xhsDao.update("t_fix_page", Chain.make("lastid", lastid).add("page", page),
						Cnd.where("type", "=", "comment"));
				logger.info("完成修复mogodb第:" + page + "页");

			}

		} catch (Exception e) {
			logger.error("", e);
		}
	}

}
